<?php
/**
 * Typecho相关文章插件
 * 
 * 
 * @package TEReadMore
 * @author arest
 * @version 1.0.0
 * @link http://www.blog.kgsoft.cn
 */
class TEReadMore_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('TEReadMore_Plugin', 'render');
    }

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){
        
    }

    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form) {
        /** 备份文件数目*/
        $num = new Typecho_Widget_Helper_Form_Element_Text('num', NULL, _t("2"), _t('文章链接样式为\'tereadmore > a\'，可以通过在css中覆盖此名称修改默认样式<br>显示相关文章的数目'));
        $form->addInput($num);
		$label = new Typecho_Widget_Helper_Form_Element_Text('label', NULL, _t("相关阅读"), _t('提示语'));
		$form->addInput($label);
    }

    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}

    /**
     * 插件实现方法
     * 
     * @access public
     * @return void
     */
    public static function render($content, $widget, $last) {
        $content = empty($last)?$content:$last;
        if ($widget->is('single')) {
            $tags = $widget->related();
            $len = $tags->length;
            if ($widget->have()) {
                $addin = "";
                $size = Typecho_Widget::widget('Widget_Options')->plugin('TEReadMore')->num;
				$label = Typecho_Widget::widget('Widget_Options')->plugin('TEReadMore')->label;
                $cnt = 0;
                while ($cnt < $size && $tags->next()) {
                    $addin .= "<a style='margin-right:10px;' href='".$tags->permalink."'>".$tags->title."</a>";
                    $cnt  = $cnt + 1;
                }
            }
            $content .= "<p class='tereadmore'>$label:<br>".$addin."</p>";
        }
        return $content;
    }
}
